# encoding: utf-8
from pymongo import MongoClient


def get_col(db_name, col_name):
    connection = MongoClient("120.27.149.15", 27000)
    db = connection[db_name]
    return db[col_name]


# db的collection插入一条纪录doc
def insert(db_name, col_name, doc):
    col = get_col(db_name, col_name)
    col.insert(doc)


# 找一个,返回的是一个doc
def find_one(db_name, col_name, query):
    col = get_col(db_name, col_name)
    return col.find_one(query)


# 找多个,返回一个list
def find_list(db_name, col_name, query):
    col = get_col(db_name, col_name)
    return col.find(query)


# current_page从1开始,最大是total/page_size取整
def get_by_page(db_name, col_name, query, current_page):
    col = get_col(db_name, col_name)
    page_size = 5
    skip_value = (current_page - 1) * page_size
    return col.find(query).skip(skip_value).limit(page_size);
